<template>
  <div class="article-comments">
    <!-- 评论列表 -->
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell
        v-for="item in list"
        :key="item.com_id.toString()"
      >
        <van-image
          slot="icon"
          round
          width="30"
          height="30"
          style="margin-right: 10px;"
          :src="item.aut_photo"
        />
        <span style="color: #466b9d;" slot="title">{{item.aut_name}}</span>
        <div slot="label">
          <p style="color: #363636;">{{item.content}}</p>
          <p>
            <span style="margin-right: 10px;">{{item.pubdate | data}}</span>
            <van-button size="mini" type="default" @click="ShowReply(item)">回复{{item.reply_count}}</van-button>
          </p>
        </div>
        <van-icon slot="right-icon" :name="item.is_liking? 'like':'like-o'" @click="like(item)" />
      </van-cell>
    </van-list>
    <!-- 评论列表 -->

    <!-- 发布评论 -->
    <van-cell-group class="publish-wrap">
      <van-field
        clearable
        placeholder="请输入评论内容"
      >
        <van-button slot="button" size="mini" type="info">发布</van-button>
      </van-field>
    </van-cell-group>
    <!-- /发布评论 -->
    <!-- 发布评论 -->
    <van-cell-group class="publish-wrap">
      <van-field clearable placeholder="请输入评论内容" v-model.trim="content">
        <van-button slot="button" size="mini" type="info" @click="add">
          发布
        </van-button>
      </van-field>
    </van-cell-group>
    <van-action-sheet
      v-model="isShowReply"
      title="评论回复"
      style="height: 60%"
    >
      <replyComment v-if="isShowReply" :comment="comment" @addCount="addCount"></replyComment>
    </van-action-sheet>
  </div>
</template>

<script>
import { addComment, disLikeComment, getComment, likeComment } from '@/api/comment'
import replyComment from './replyComment.vue'
export default {
  name: 'ArticleComment',
  data () {
    return {
      list: [], // 评论列表
      loading: false, // 上拉加载更多的 loading
      finished: false, // 是否加载结束
      offset: null,
      content: '',
      isShowReply: false,
      comment: {}
    }
  },

  methods: {
    async onLoad () {
      const res = await getComment(this.$route.params.id, this.offset)
      this.offset = res.data.last_id
      this.list = [...this.list, ...res.data.results]
      this.loading = false
      if (res.data.results.length < 10) {
        this.finished = true
      }
    },
    async add () {
      if (!this.content) return this.$toast('评论不能为空')
      const res = await addComment(this.$route.params.id, this.content)
      this.$toast.success('评论成功')
      this.content = ''
      this.list.unshift(res.data.new_obj)
    },
    async like (item) {
      if (item.is_liking) {
        await disLikeComment(item.com_id)
      } else {
        await likeComment(item.com_id)
      }
      this.$toast.success('操作成功')
      item.is_liking = !item.is_liking
    },
    ShowReply (item) {
      this.isShowReply = true
      this.comment = item
    },
    addCount () {
      this.comment.reply_count++
    }
  },
  components: {
    replyComment
  }
}
</script>

<style scoped lang='less'>
// 发表评论的区域是固定在下端的
.publish-wrap {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
}
// 给发表评论区空出地方
.van-list {
  margin-bottom: 45px;
}
</style>
